home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2 - Developers' Solutions / Delphi 2 Developers' Solutions.iso / dds / chap07 / howto06 / delphi10 / soundex / soundfrm.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-09-17  |  3.1 KB  |  107 lines

  1. unit Soundfrm;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables,
  8.   Memtable, Scanners, Soundex;
  9.  
  10. type
  11.   TEmployeeSearchForm = class(TForm)
  12.     EmployeeDataSource: TDataSource;
  13.     MatchingDataSource: TDataSource;
  14.     EmployeeTable: TTable;
  15.     EmployeeDBGrid: TDBGrid;
  16.     MatchingDBGrid: TDBGrid;
  17.     SearchEdit: TEdit;
  18.     SoundexSearchButton: TButton;
  19.     procedure SoundexSearchButtonClick(Sender: TObject);
  20.   private
  21.     { Private declarations }
  22.     MatchingInMemoryTable: TInMemoryTable;
  23.   public
  24.     { Public declarations }
  25.   end;
  26.  
  27. var
  28.   EmployeeSearchForm: TEmployeeSearchForm;
  29.  
  30. implementation
  31.  
  32. {$R *.DFM}
  33.  
  34. { Table scanner's tranfer condition function. }
  35. function SoundexMatch( SearchTable: TTable ): Boolean; far;
  36. begin
  37.   { Compare soundex code of current record's LastName field
  38.     with the soundex code of the user provided text.
  39.     Notice that you have to reference the EmployeeSearchForm
  40.     explicitly, since this is not a method of that form, but
  41.     a free, utility function. }
  42.   if SoundexCode( SearchTable.FieldByName('LastName').AsString )
  43.     = SoundexCode( EmployeeSearchForm.SearchEdit.Text )
  44.   then Result := True
  45.   else Result := False;
  46. end;
  47.  
  48. { Table scanner's transfer action procedure. }
  49. procedure TransferMatchingRecord(
  50.   SourceTable, DestinationTable: TTable
  51. ); far;
  52. var
  53.   i: Integer;
  54. begin
  55.   { Assume SourceTable and DestinationTable have the
  56.     same structure. Make a field-by-field copy. }
  57.   DestinationTable.Append;
  58.   for i := 0 to SourceTable.FieldCount - 1 do
  59.   begin
  60.     DestinationTable.Fields[ i ].AsString :=
  61.       SourceTable.Fields[ i ].AsString;
  62.   end;
  63. end;
  64.  
  65. procedure TEmployeeSearchForm.SoundexSearchButtonClick(
  66.   Sender: TObject);
  67. var
  68.   MatchingScanner: TTableConditionalTransferScanner;
  69. begin
  70.   MatchingDataSource.DataSet := Nil;
  71.   { If nothing to search, don't bother further. }
  72.   if SearchEdit.Text = '' then exit;
  73.   { If the temporary in-memory table is alive, kill it. }
  74.   if MatchingInMemoryTable <> Nil then
  75.   begin
  76.     MatchingInMemoryTable.Close;
  77.     MatchingInMemoryTable.Free;
  78.     MatchingInMemoryTable := Nil;
  79.   end;
  80.   { Make sure the Employee table is open. }
  81.   EmployeeTable.open;
  82.   { Create new temporary in-memory table. The table will be
  83.     empty, ready to accept records that match with
  84.     the user provided search criteria. }
  85.   MatchingInMemoryTable := TInMemoryTable.CreateLike(
  86.     EmployeeTable,
  87.     'Mem',
  88.     Self
  89.   );
  90.   { Link in-memory table to grid via MatchingDataSource. }
  91.   MatchingInMemoryTable.Open;
  92.   MatchingDataSource.DataSet := MatchingInMemoryTable;
  93.   { Create an appropriate table scanner. }
  94.   MatchingScanner := TTableConditionalTransferScanner.Create(
  95.     EmployeeTable,              { Source Table }
  96.     MatchingInMemoryTable,      { Destination Table }
  97.     TransferMatchingRecord,     { Transfer Action }
  98.     SoundexMatch                { Transfer Condition }
  99.   );
  100.   { Scan through empolyee table, transferring sound alikes
  101.     to temporary in-memory table. }
  102.   MatchingScanner.Execute;
  103.   MatchingScanner.Free;
  104. end;
  105.  
  106. end.
  107.